
const TerserPlugin = require('terser-webpack-plugin')  // 用于在生成环境剔除debuger和console
const CompressionPlugin = require("compression-webpack-plugin"); // gzip压缩,优化http请求,提高加载速度
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin // 代码分析工具
const copyWebpackPlugin = require('copy-webpack-plugin');//静态资源拷贝
const path = require('path');
const resolve = dir => {
  return path.join(__dirname, dir);
};

// const env = process.env.NODE_ENV
// let target = process.env.VUE_APP_URL  // development和production环境是不同的

const cdn = {
  // 开发环境
  dev: {
      css: [],
      js: [
      ]
  },
  // 生产环境
  build: {
//    css: [
//      'https://cdnjs.cloudflare.com/ajax/libs/element-ui/2.11.1/theme-chalk/index.css',
//      'https://cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.css'
//    ],
//    js: [
//      'https://cdnjs.cloudflare.com/ajax/libs/vue/2.6.10/vue.min.js',
//      'https://cdnjs.cloudflare.com/ajax/libs/vue-router/3.1.2/vue-router.min.js',
//      'https://cdnjs.cloudflare.com/ajax/libs/vuex/2.3.1/vuex.min.js',
//      'https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.min.js',
//      'https://cdnjs.cloudflare.com/ajax/libs/vue-i18n/8.13.0/vue-i18n.min.js',
//      'https://cdnjs.cloudflare.com/ajax/libs/element-ui/2.11.1/index.js',
//      'https://cdnjs.cloudflare.com/ajax/libs/echarts/3.8.5/echarts.min.js',
//      'https://cdnjs.cloudflare.com/ajax/libs/Mock.js/1.0.1-beta3/mock-min.js',
//      'https://cdnjs.cloudflare.com/ajax/libs/nprogress/0.2.0/nprogress.min.js',
//      'https://cdnjs.cloudflare.com/ajax/libs/js-cookie/2.2.0/js.cookie.min.js'
//    ]
    css: [
      '/static/css/element-ui/2.11.1/theme-chalk/index.css',
      '/static/css/nprogress/0.2.0/nprogress.min.css'
    ],
    js: [
      '/static/js/vue/2.6.10/vue.min.js',
      '/static/js/vue-router/3.1.2/vue-router.min.js',
      '/static/js/vuex/2.3.1/vuex.min.js',
      '/static/js/axios/0.19.0/axios.min.js',
      '/static/js/vue-i18n/8.13.0/vue-i18n.min.js',
      '/static/js/element-ui/2.11.1/index.js',
      '/static/js/echarts/3.8.5/echarts.min.js',
      '/static/js/Mock.js/1.0.1-beta3/mock-min.js',
      '/static/js/nprogress/0.2.0/nprogress.min.js',
      '/static/js/js-cookie/2.2.0/js.cookie.min.js'
    ]
  }
}


module.exports = {
  publicPath: process.env.NODE_ENV === "production" ? "/" : "/",
  outputDir: './dist',
  assetsDir:'static',
  filenameHashing:true, // false 来关闭文件名哈希
  lintOnSave: false, // 关闭eslint
  // 打包时不生成.map文件
  productionSourceMap: false,
  devServer: {
    open: true,
    host: '0.0.0.0',
    port: 8001,
    // 由于本项目数据通过easy-mock和mockjs模拟，不存在跨域问题，无需配置代理;
    proxy: {
      /**
       * 代理配置
       * 1. 如果target属性设置为本地环境，则需要把pathRewrite属性值置空。
       * 2. 如果target属性设置为测试环境，则需要把pathRewrite属性值预留。
       */
      // 服务器代理 cl4 环境
      // '/ilmp': {
      //   target: 'http://10.100.74.33:30024',
      //   changeOrigin: true,
      //   pathRewrite: {
      //     '^/ilmp': '/ilmp'
      //   }
      // },
			'/ilmp/': {
        // target: 'http://10.100.74.33:30024',
        // target: 'http://localhost:10001',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
      },
      '/CITICGIS': {
          // target: 'http://localhost:8080',
          // target: 'http://128.232.6.242:9080',
					target: 'http://10.30.38.76:10088',
          changeOrigin: true,
          pathRewrite: {
          '^/CITICGIS': '/CITICGIS'
          }
      },
      '/ilmp-pe-app': {
        // target: 'http://localhost:10027',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp-pe-app': ''
        }
      },

      '/ilmp/system-app': {
        // target: 'http://10.100.74.33:30024',
        // target: 'http://localhost:10001',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp/system-app': '/ilmp/system-app'
        }
      },
      '/ilmp-scylog-app': {
        // target: 'http://10.100.74.33:30024',
        // target: 'http://localhost:10001',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp-scylog-app': '/ilmp-scylog-app'
        }
      },
      '/ilmp/task-app': {
        // target: 'http://10.100.74.33:30024',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp/task-app': '/ilmp/task-app'
        }
      },
      '/ilmp/nb-app': {
        // target: 'http://localhost:10006',
        // target: 'http://10.100.74.33:30024',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp/nb-app': '/ilmp/nb-app'
        }
      },
      '/ilmp/new-undr-app': {
        // target: 'http://localhost:10015',
        // target: 'http://10.100.74.33:30024',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp/new-undr-app': '/ilmp/new-undr-app'
        }
      },
      '/ilmp/given-undr-app': {
        // target: 'http://localhost:10017',
        // target: 'http://10.100.74.33:30024',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp/given-undr-app': '/ilmp/given-undr-app'
        }
      },
      '/ilmp/pos-app': {
        // target: 'http://localhost:10010',
        // target: 'http://10.100.74.33:30024',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp/pos-app': '/ilmp/pos-app'
        }
      },
      '/ilmp/insure-app': {
        // target: 'http://localhost:10014',
        // target: 'http://10.100.74.33:30024',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp/insure-app': '/ilmp/insure-app'
        }
      },
      '/ilmp/amount-app': {
        // target: 'http://localhost:10011',
        // target: 'http://10.100.74.33:30024',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp/amount-app': '/ilmp/amount-app'
        }
      },
      '/ilmp/premium-app': {
        // target: 'http://localhost:10013',
        // target: 'http://10.100.74.33:30024',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp/premium-app': '/ilmp/premium-app'
        }
      },
      '/ilmp/customer-app': {
        // target: 'http://localhost:10002',
        // target: 'http://10.100.74.33:30024',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp/customer-app': '/ilmp/customer-app'
        }
      },
      '/ilmp-hqnr-app': {
        // target: 'http://localhost:10002',
        // target: 'http://10.100.74.33:30024',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp-hqnr-app': '/ilmp-hqnr-app'
        }
      },
      '/ilmp-clmbase-app': {
        // target: 'http://localhost:10037',
        // target: 'http://10.100.74.33:30024',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp-clmbase-app': '/ilmp-clmbase-app'
        }
      },
      '/ilmp-aus-config-app': {
        // target: 'http://localhost:10037',
        // target: 'http://10.100.74.33:30024/ilmp-aus-config-app',
				target: 'http://10.30.38.76:10088/ilmp-aus-config-app',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp-aus-config-app': ''
        }
      },
      '/ilmp-aus-main-app': {
        // target: 'http://localhost:10037',
        // target: 'http://10.100.74.33:30024',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp-aus-main-app': '/ilmp-aus-main-app'
        }
      },
      '/ilmp-pre-undr-app': {
        // target: 'http://localhost:10055',
        // target: 'http://10.100.74.33:30024',
				target: 'http://10.30.38.76:10088',
        changeOrigin: true,
        pathRewrite: {
          '^/ilmp-pre-undr-app': '/ilmp-pre-undr-app'
        }
      },
    }
  },
   // webpack相关配置
  chainWebpack: (config) => {
    config.entry.app = ['./src/main.js']
    config.resolve.alias
      .set('@', resolve('src'))
      .set('cps', resolve('src/components'))
    // 关闭npm run build之后，This can impact web performance 警告
    config.performance
      .set('hints', false)
    // 移除 prefetch 插件
    config.plugins.delete("prefetch");
    // 移除 preload 插件
    config.plugins.delete('preload');
    // 压缩代码
    config.optimization.minimize(true);
    // 分割代码
    config.optimization.splitChunks({
        chunks: 'all'
    })
    // 对图片进行压缩处理
    config.module
    .rule('images')
    .use('image-webpack-loader')
    .loader('image-webpack-loader')
    .options({
        disable: true, // webpack@2.x and newer
        quality: '65-80',
        speed: 4
    })
    .end()

    config.module
    .rule('svg')
    .uses.clear()
  config.module
    .rule('svg1')
    .test(/\.svg$/)
    .use('svg-sprite')
    .loader('svg-sprite-loader')
    .options({
      symbolId: 'icon-[name]'
    })
    .end()
    .include
    .add(path.join(__dirname,'src/icons'))
    .end()

    // 项目文件大小分析
    // config.plugin('webpack-bundle-analyzer')
    // .use(new BundleAnalyzerPlugin({
    //   openAnalyzer: true,   // 是否打开默认浏览器
    //   analyzerPort:9001
    // }))

    // 对vue-cli内部的 webpack 配置进行更细粒度的修改。
    // 添加CDN参数到htmlWebpackPlugin配置中， 详见public/index.html 修改
    config
    .plugin('html')
    .tap(args => {
      if (process.env.NODE_ENV === 'production') {
          args[0].cdn = cdn.build
      }
      if (process.env.NODE_ENV === 'development') {
          args[0].cdn = cdn.dev
      }
      return args
    })
  },
  configureWebpack:config => {
    // 为生产环境修改配置...
    if (process.env.NODE_ENV === 'production') {
      // 忽略生产环境打包的文件
      config.externals = {
        "vue": "Vue",
        "vue-router": "VueRouter",
        "vuex": "Vuex",
        "vue-i18n": "VueI18n",
        "axios": "axios",
        'element-ui': 'ELEMENT',
        'echarts':'echarts',
        'mockjs':'Mock',
        'nprogress':'NProgress',
        'js-cookie':'Cookies'
      }
      // 去除console来减少文件大小，效果同'UglifyJsPlugin'
      new TerserPlugin({
        cache: true,
        parallel: true,
        sourceMap: true, // Must be set to true if using source-maps in production
        terserOptions: {
          compress: {
            warnings: false,
            drop_console: true,
            drop_debugger: true,
            pure_funcs: ['console.log']
          }
        }
      })
      //拷贝静态资源
      config.plugins.push(new copyWebpackPlugin([{
        from: __dirname + '/src/assets',//打包的静态资源目录地址
        to: __dirname + '/dist/static' //打包到dist下面的public
      }]))
      // 开启gzip压缩
      config.plugins.push(new CompressionPlugin({
        algorithm: 'gzip',
        test: new RegExp("\\.(" + ["js", "css"].join("|") + ")$"), // 匹配文件扩展名
        // threshold: 10240, // 对超过10k的数据进行压缩
        threshold: 5120, // 对超过5k的数据进行压缩
        minRatio: 0.8,
        cache: true, // 是否需要缓存
        deleteOriginalAssets:false  // true删除源文件(不建议);false不删除源文件
      }))

    } else {
      // 为开发环境修改配置...

    }
  },
   // 第三方插件配置
  pluginOptions: {

  }
}

